package com.ph.elec.gwt.post.server;

import javax.jdo.PersistenceManager;

import org.apache.commons.lang.Validate;

import com.google.appengine.api.datastore.Text;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ph.elec.data.FrontNews;
import com.ph.elec.gwt.post.client.PostService;

import dan.util.dstore.PersistenceUtils;

public class PostServiceImpl extends RemoteServiceServlet 
		implements PostService {
	
	private static final long serialVersionUID = -8001628122972831248L;
	
	public boolean FrontNews(FrontNews post) {
		Validate.notNull(post);
		if (!isValid(post)) {
			return false;
		}
		FrontNewsEntity entity = new FrontNewsEntity();
		entity.setBody(new Text(post.getBody()));
		entity.setKeywords(post.getKeywords());
		entity.setPoster(post.getPoster());
		entity.setSnippet(new Text(post.getSnippet()));
		entity.setTimePost(post.getTimePost());
		entity.setTitle(post.getTitle());

		PersistenceManager db = PersistenceUtils.mgr();
		try {
			db.makePersistent(entity);
		} finally {
			db.close();
		}
		return true;
	}

	private boolean isValid(FrontNews post) {
		Validate.notEmpty(post.getBody());
		Validate.notEmpty(post.getPoster());
		Validate.notEmpty(post.getSnippet());
		Validate.notEmpty(post.getTitle());
		return true;
	}
}
